﻿using Newtonsoft.Json;
using UnityEngine;

/// <summary>
///     PlayerPrefs数据管理类 统一管理数据的存储和读取
/// </summary>
public class PlayerPrefsEncryptDataMgr : SingletonBase<PlayerPrefsEncryptDataMgr>
{
    public void SaveData<T>(T data, string keyName)
    {
        var jsonStr = JsonConvert.SerializeObject(data);
        jsonStr = StringUtil.Base64Encrypt(jsonStr);
        PlayerPrefs.SetString(keyName, jsonStr);
    }


    public T LoadData<T>(string keyName) where T : new()
    {
        var jsonStr = PlayerPrefs.GetString(keyName);
        if (jsonStr == "") return new T();
        jsonStr = StringUtil.Base64Decrypt(jsonStr);
        var data = JsonConvert.DeserializeObject<T>(jsonStr);
        return data;
    }
}